package com.augurit.gzcgwsp.ywcmn.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.util.PageUtils;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.lang.StringUtils;

import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.Page;
import com.augurit.gzcgwsp.ywcmn.convert.YwComClxxConvertor;
import com.augurit.gzcgwsp.ywcmn.dao.YwComClxxDao;
import com.augurit.gzcgwsp.ywcmn.entity.YwComClxx;
import com.augurit.gzcgwsp.ywcmn.service.IYwComClxxService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComClxxForm;

@Service
@Transactional
public class YwComClxxServiceImpl implements IYwComClxxService{

	@Autowired
	private YwComClxxDao ywComClxxDao;
	
	@Transactional(readOnly = true)
	public YwComClxxForm get(Long id) {
		YwComClxx entity = ywComClxxDao.get(id);
		YwComClxxForm ywComClxxForm = YwComClxxConvertor.convertVoToForm(entity);
		if(ywComClxxForm != null){
			String sql = "select t.sqdw from YW_COM_QYXX t where t.id ='"+ywComClxxForm.getFkid()+"'";
			List<String> sqdwMclist = ywComClxxDao.getSession().createSQLQuery(sql).list();
			if(sqdwMclist != null){
				ywComClxxForm.setQymc(sqdwMclist.get(0));
			}
		}		
		return ywComClxxForm;
	}
	
	public void delete(Long... ids) {
		ywComClxxDao.delete(ids);
	}

	public void save(YwComClxxForm... forms) {
		if(forms != null)
			for(YwComClxxForm form : forms)
				this.save(form);
	}
	
	public void save(YwComClxxForm form){
		
		if(form != null){
			YwComClxx entity = null;
			
			if(form != null && form.getId() != null){
				entity = ywComClxxDao.get(form.getId());
			}else{
				entity = new YwComClxx();
			}
			
			YwComClxxConvertor.convertFormToVo(form, entity);
			
			ywComClxxDao.save(entity);
			
			form.setId(entity.getId());
		}
	}

	@Transactional(readOnly = true)
	public Page<YwComClxxForm> search(Page<YwComClxxForm> page, YwComClxxForm form) {
		Page pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("select ps from YwComClxx ps , YwComQyxx qyxx where 1=1  and ps.fkid = qyxx.id and qyxx.templateCode like '%jzljczz%'");
		List values = new ArrayList();
		if(form != null){

			if(StringUtils.isNotBlank(form.getQymc())){
				hql.append(" and qyxx.sqdw like '%"+form.getQymc()+"%'");
			}
		    if (StringUtils.isNotBlank(form.getClph())) {
		        hql.append(" and ps.clph like '%" + form.getClph() + "%'");
		    }
            if (StringUtils.isNotBlank(form.getZybh())) {
                hql.append(" and ps.zybh like '%" + form.getZybh() + "%'");
            }
            if (StringUtils.isNotBlank(form.getSfhg2())) {
                hql.append(" and ps.sfhg2 = '" + form.getSfhg2() + "'");
            }
		}
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		ywComClxxDao.findPage(pg, hql.toString(), values);		
		List<YwComClxxForm> list = YwComClxxConvertor.convertVoListToFormList(pg.getResult());
		if(list != null && list.size() > 0){
			for(int i = 0;i < list.size();i ++){
				YwComClxxForm ywComClxxForm = list.get(i);
				System.out.println("ywComClxxForm.getFkid() = "+ywComClxxForm.getFkid());
				String sql = "select t.sqdw from YW_COM_QYXX t where t.id ='"+ywComClxxForm.getFkid()+"'";
				List<String> sqdwMclist = ywComClxxDao.getSession().createSQLQuery(sql).list();
				if(sqdwMclist != null){
					ywComClxxForm.setQymc(sqdwMclist.get(0));
				}
			}
		}
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	@Transactional(readOnly = true)
	public Page<YwComClxxForm> search(Page<YwComClxxForm> page, List<PropertyFilter> filters) {
		Page<YwComClxx> pg = PageUtils.newPageInstance(page);
		
		ywComClxxDao.findPage(pg, filters);
		
		List<YwComClxxForm> list = YwComClxxConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
}
